package com.csi.sai.bl.dataview;

import java.util.ArrayList;

import javax.sql.DataSource;

import com.csi.sai.data.entities.Contacto;
import com.csi.sai.utilities.templates.reports.ContactTemplate;

public class GetDataViewContacts extends GetDataView {
	private String htmlHeader;
	private String htmlBody;

	public GetDataViewContacts(DataSource ds) {
		super(ds);
		
	}

	@Override
	protected void writeHeaders() {
		htmlHeader = ContactTemplate.headers;
	}

	@Override
	protected <T> void writeBody(Generics<T> object) {
		StringBuffer sb = new StringBuffer();
		ArrayList<Contacto> listaContactos = (ArrayList<Contacto>) object.getObject();
		int index = 0;
		for(Contacto contacto : listaContactos) {
			String row = ContactTemplate.row;
			row = row.	replace("{idContact}", ""+contacto.getIdContacto()).
						replace("{name}", contacto.getNombreContacto()).
						replace("{pat}", contacto.getApellidoPaterno()).
						replace("{mat}", contacto.getApellidoMaterno()).
						replace("{client}", contacto.getNombre()).
						replace("{address}", contacto.getDireccion()).
						replace("{title}", "Contacto "+contacto.getIdContacto()).
						replace("{idClient}", ""+contacto.getIdCliente())
						.replace("{index}", ""+index);
			sb.append(row);
			index++;
		}
		htmlBody = ContactTemplate.body.replace("{body}", sb.toString());
	}

	@Override
	public String getHTMLCode() {
		
		return htmlHeader.replace("{body}", htmlBody);
	}

}
